var api = require('../../util/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isEdit: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this;
    if(api.isBlank(options.id)){
      _this.setData({
        isEdit: false,
        item: {},
        imgList: []
      });
    }else{
    api.get(`${api.url.ldjlb}/${options.id}`).then(res => {
      var pic = res.data.pic;
      if(api.isBlank(pic)){
        _this.setData({
          item: res.data,
          imgList: []
        });
      }
      api.preBase64Img(pic, function (imgMap) {
        var imgList = [];
        Object.keys(imgMap).forEach(function (key) {
          imgList.push({
            base64: imgMap[key],
            fileId: key
          });
        });
        _this.setData({
          item: res.data,
          imgList: imgList
        });
      })
    })
    }
  },
  showImg(e) {
    var _this = this;
    var index = e.currentTarget.dataset.index;
    wx.previewImage({
      urls: _this.data.imgList,
      current: _this.data.imgList[index]
    })
  },
  // 删除图片
  clearImg: function (e) {
    var index = e.currentTarget.dataset.index;
    var nowList = []; //新数据
    var imgList = this.data.imgList; //原数据

    for (let i = 0; i < imgList.length; i++) {
      if (i == index) {
        continue;
      } else {
        nowList.push(imgList[i])
      }
    }
    this.setData({
      imgList: nowList
    })
  },
  //上传图片
  upload: function (e) {
    var that = this;
    wx.chooseImage({
      count: 2 - that.data.imgList.length,
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        let imgList = that.data.imgList;
        res.tempFilePaths.forEach(function(tempFilePath){
          imgList.push({
            base64: tempFilePath,
            fileId: ''
          });
        });
        that.setData({
          imgList: imgList
        })
      }
    })
  },
  formSubmit(e) {
    var _this = this;
    var params = e.detail.value;
    var pic = [];
    var uploadPic = [];
    var imgList = this.data.imgList; //原数据
    for (let i = 0; i < imgList.length; i++) {
      var fileId = imgList[i].fileId;
      var base64 = imgList[i].base64;
      if (fileId == '') {
        uploadPic.push(base64);
      } else {
        pic.push(fileId);
      }
    }
    if(!_this.data.isEdit){
      params.examineId = _this.options.examineId;
      params.userId = _this.options.userId;
      params.userName = _this.options.userName;
      if(uploadPic.length == 0){
        api.post(api.url.ldjlb, params).then(res => {
          api.back("新增成功", "success");
        })
      }else{
        api.uploadFile(uploadPic, params.id, function (res) {
          res.forEach(function (file) {
            pic.push(file.fileId);
          })
          params.pic = pic.join(',');
          api.post(api.url.ldjlb, params).then(res => {
            api.back("新增成功", "success");
          })
        });
      }
      return;
    }
    if (uploadPic.length == 0 && pic.length == 2) {
      api.put(api.url.ldjlb, params).then(res => {
        api.back("修改成功", "success");
      })
      return;
    }

    if (uploadPic.length == 0 && pic.length < 2) {
      params.pic = pic.join(",");
      api.put(api.url.ldjlb, params).then(res => {
        api.back("修改成功", "success");
      })
      return;
    }

    api.uploadFile(uploadPic, params.id, function (res) {
      res.forEach(function (file) {
        pic.push(file.fileId);
      })
      params.pic = pic.join(',');
      api.put(api.url.ldjlb, params).then(res => {
        api.back("修改成功", "success");
      })
    });
  }
})